package com.datang.pet.api.givePetController;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-24 08:17
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.redis.RedisTool;
import com.datang.pet.common.response.Result;
import com.datang.pet.common.utils.MD5;
import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.pojo.City;
import com.datang.pet.service.GivePetService;
import com.datang.pet.vo.AddGivePetRequest;
import com.datang.pet.vo.GetGivePetListByCityRequest;
import com.datang.pet.vo.GetTalkListResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class givePetController {

    @Autowired
    private GivePetService givePetService;

    @Autowired
    private RedisTool redisTool;

    @PostMapping("/addGivePet")
  public Result addGivePet(@RequestBody AddGivePetRequest addGivePetRequest)throws Exception{

        if (!StringUtils.isNUll(addGivePetRequest.getUserId())) {
            throw new Exception("用户id为空");
        }

        if (addGivePetRequest.getUrlList()==null) {
            throw new Exception("资源为空");
        }

        if (!StringUtils.isNUll(addGivePetRequest.getPetDescribe())) {
            throw new Exception("宠物描述为空");
        }

        if (!StringUtils.isNUll(addGivePetRequest.getAdoptClaim())) {
            throw new Exception("领养要求为空");
        }

        if (!StringUtils.isNUll(addGivePetRequest.getSterilization())) {
            throw new Exception("是否绝育为空");
        }

        if (!StringUtils.isNUll(addGivePetRequest.getVaccine())) {
            throw new Exception("是否疫苗为空");
        }

        if (!StringUtils.isNUll(addGivePetRequest.getExpelling())) {
            throw new Exception("是否驱虫为空");
        }

        if (!StringUtils.isNUll(addGivePetRequest.getProvince())) {
            throw new Exception("省为空");
        }

        if (!StringUtils.isNUll(addGivePetRequest.getCity())) {
            throw new Exception("市为空");
        }

        if (!StringUtils.isNUll(addGivePetRequest.getDistrict())) {
            throw new Exception("区为空");
        }
        if (!StringUtils.isNUll(addGivePetRequest.getFormId())) {
            throw new Exception("通知id为空");
        }

        try {
            addGivePetRequest.setUserId(MD5.decryptData(addGivePetRequest.getUserId()));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }

        givePetService.AddGivePet(addGivePetRequest);

        return Result.ok("ok");
  }

    @PostMapping("/getGivePetListByCity")
    public Result getGivePetListByCity(@RequestBody GetGivePetListByCityRequest getGivePetListByCityRequest)throws Exception{
       return Result.ok(givePetService.getGivePetListByCity(getGivePetListByCityRequest.getCity(),getGivePetListByCityRequest.getTime())) ;
    }

    @GetMapping("/getCityList")
    public Result getCityList()throws Exception{

        List<City> list =(List<City>) redisTool.get("citylist");
        if(list!=null){
            return Result.ok(list) ;
        }else{
            return Result.ok(givePetService.getCityList()) ;
        }


    }

    @GetMapping("/getGiveInfo")
    public Result getGiveInfo(String id)throws Exception{
        return Result.ok(givePetService.getGiveInfo(id));
    }

    @GetMapping("/getGiveListByUserId")
    public Result getGiveListByUserId(String userId)throws Exception{



        //从redis中取用户喵文
        ArrayList talkList = (ArrayList<GetTalkListResponse>) redisTool.get("giveListByUserId" + userId);
        if (talkList == null) {
            //取不到则到mysql中取
            talkList = (ArrayList) givePetService.getGiveListByUserId(userId);

            redisTool.set("giveListByUserId" + userId, talkList);
        }
        return Result.ok(talkList);
    }

    @GetMapping("/deleteGiveById")
    public Result deleteGiveById(String userId,String id)throws Exception{
        try {
            userId = (MD5.decryptData(userId));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }
        givePetService.deleteGiveById(userId,id);
        return Result.ok("ok");
    }
}
